<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    //1.函数参数默认值
    function add(a, b) {
      // if(!b) {
      //   b =  1;
      // }
      b = b || 1;
      return a + b;
    }
    console.log(add(3));
    console.log(add(3, 4));

    function add(a, b=1) {
      return a + b;
    }
    console.log(add(3));
    console.log(add(3, 4));

    //不定参数，可变参数
    function fun(...values) {
      console.log(values.length);
    }
    fun(2, 4);
    fun(4, 5, 6);

    //2.箭头函数，ES6中函数简写形式
    //2.1传递一个参数
    let print1 = function(obj) {
      console.log(obj);
    }
    //简写成
    let print2 = (obj) => {
      console.log(obj);
    };
    let print3 = obj => console.log(obj);
    
    //2.2多个参数
    let sum1 = function(a, b) {
      return a + b;
    }
    //简写
    let sum2 = (a, b) => {
      return a + b;
    }
    let sum3 = (a, b) => a + b;

    //2.3对象函数简写
    let person = {
      name: 'zhangsan',
      //以前
      eat1: function(food) {
        console.log(this.name + '在吃' + food);
      },
      //箭头函数:这里拿不到this
      eat2: food => console.log(person.name + '在吃' + food),
      //函数简写
      eat3(food) {
        console.log(this.name + '在吃' + food);
      }
    };
    person.eat1('肉');//zhangsan在吃肉
    person.eat2('肉');//在吃肉
    person.eat3('肉');//zhangsan在吃肉
  </script>
</body>
</html>